package com.guldan.core.response;

/**
 * Created by Silas.
 * Date: 2016/10/1
 * Time: 22:01
 */

import com.guldan.core.jackson.UnitSuffixSerialize;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
import java.io.IOException;
import java.text.SimpleDateFormat;

public class JsonObjectMapper extends ObjectMapper {
    private static final long serialVersionUID = 4101422916858447072L;

    public JsonObjectMapper() {
        this.getSerializerProvider().setNullValueSerializer(new JsonSerializer() {
            public void serialize(Object value, JsonGenerator generator, SerializerProvider provider) throws IOException, JsonProcessingException {
                generator.writeString("");
            }
        });
        this.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        SimpleModule module = new SimpleModule("unitSuffixSerialize", Version.unknownVersion());
        module.addSerializer(Number.class, new UnitSuffixSerialize());
        module.addSerializer(Integer.TYPE, new UnitSuffixSerialize());
        this.registerModule(module);
        this.setFilters((new SimpleFilterProvider()).setFailOnUnknownId(false));
    }
}

